public class MySingleList {

    static class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;

    public void createLinkedList(){
        Node node1 = new Node(17);
        Node node2 = new Node(7);
        Node node3 = new Node(167);
        Node node4 = new Node(46);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    public void display() {
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        Node cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        //如果链表中一个元素都没有时：
        if(head == null){
            head = node;
            return ;
        }
        //链表中存在一个或以上的元素时：
        Node cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new IndexOutOfBoundException("index 位置不合法，请检查下标的合法性！");
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        Node cur = head;
        Node node = new Node(data);
        //如果 index 是 5 ，要找下标为 4 的节点的地址，而下标为 4 节点的地址，存储在下标为 3 节点的next 里：
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //如果一个元素都没有时：
        if(head == null){
            return;
        }
        //如果删除的是头元素：
        if(head.val == key){
            head = head.next;
            return;
        }
//        Node cur = head;
//        Node prev = preContains(key);
//        //如果删除的是最后一个元素：
//        if(prev.next.next == null){
//            prev.next = null;
//            return;
//        }
//        prev.next = prev.next.next;
        //修改：
        Node cur = preContains(key);
        //没有找到指定删除的元素：
        if(cur == null){
            return;
        }
        //找到要删除的元素时:
        Node del = cur.next;
        cur.next = del.next;
    }

    private Node preContains(int key){
        Node cur = head;
        while(cur.next != null){
            if(cur.next.val != key){
                cur = cur.next;
            }
            return cur;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        //添加的部分：
        //链表为空时:
        if(head == null){
            return;
        }
        Node prev = head;
        Node cur = prev.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }

    public void clear() {
        head = null;
    }
}


